import axios from "axios";
import router from "../router";

const request = axios.create({
  baseURL: "https://m1.apifoxmock.com/m1/5455088-5130247-default",
  // baseURL: "/api",
  timeout: 5000,
  headers: { Terminal: "h5" }
} as any);

request.interceptors.request.use((req: any) => {
  // console.log(req.url);
  return req;
});

request.interceptors.response.use(
  (res: any) => {
    if (res.data.apifoxError && res.data.apifoxError.code === "404") {
      router.replace('/');
      return Promise.reject(new Error("404 Not Found"));
    }
    return res.data;
  },
  (err: any) => {
    if (err.response && err.response.status === 404) {
      router.replace('/');
      return Promise.reject(err);
    }
    return Promise.reject(err);
  }
);

export default request;
